//
//  zzqTabBar.m
//  zzqKanBoKe
//
//  Created by ben on 17/2/2.
//  Copyright © 2017年 张增强. All rights reserved.
//

#import "zzqTabBar.h"

@interface zzqTabBar()

@property (nonatomic, strong)UIImageView * tabbgView;

@property (nonatomic, strong)NSArray * dataList;

@property(nonatomic, strong)UIButton * lastItem;

@property (nonatomic, strong) UIButton * cameraButton;

@end

@implementation zzqTabBar

-(UIButton *)cameraButton{
    if(!_cameraButton){
        _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
        [_cameraButton addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        _cameraButton.tag = ZZQItemTypeLaunch;
        _cameraButton.adjustsImageWhenHighlighted = NO;
    }
    return _cameraButton;
}

- (NSArray *)dataList{
    if(!_dataList){
        _dataList = @[@"tab_live", @"tab_me"];
    }
    return _dataList;
}

- (UIImageView *)tabbgView{
    if(!_tabbgView){
        _tabbgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"global_tab_bg"]];
    }
    return _tabbgView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tabbgView];
        for (NSInteger i = 0; i < self.dataList.count; i++) {
            UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
            
            //高亮状态不会自适应图片
            item.adjustsImageWhenHighlighted = NO;
            [item setImage:[UIImage imageNamed:self.dataList[i]] forState:UIControlStateNormal];
            [item setImage:[UIImage imageNamed:[self.dataList[i] stringByAppendingString:@"_p"]] forState:UIControlStateSelected];
            item.tag = ZZQItemTypeLive+i;
            
            [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
            
            if(i == 0){
                item.selected = YES;
                self.lastItem = item;
            }
            
            [self addSubview:item];
        }
        //添加直播按钮
        [self addSubview:self.cameraButton];
    }
    return self;
}

- (void)clickItem:(UIButton *)item{
    if([self.delegate respondsToSelector:@selector(zzqTabBar:clickButton:)]){
        [self.delegate zzqTabBar:self clickButton:item.tag];
    }
    !self.block?:self.block(self, item.tag);
    
    if(item.tag != ZZQItemTypeLaunch){
        //直播，跳转模态视图
        self.lastItem.selected = NO;
        item.selected = YES;
        self.lastItem = item;
    }
    
    
    //设置button动画
    [UIView animateWithDuration:0.2 animations:^{
        item.transform = CGAffineTransformMakeScale(1.2, 1.2);
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            item.transform = CGAffineTransformIdentity;
        }];
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.tabbgView.frame = self.bounds;
    CGFloat width = self.bounds.size.width / self.dataList.count;
    for (NSInteger i = 0; i < [self subviews].count; i++) {
        UIView * btn = [self subviews][i];
        if([btn isKindOfClass:[UIButton class]]){
            btn.frame = CGRectMake((btn.tag-ZZQItemTypeLive)*width, 0, width, self.frame.size.height);
        }
    }

    [_cameraButton sizeToFit];
    self.cameraButton.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height-50);
}

@end
